﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AssemblyCYG.参数模块
{
    public static class FileOperate
    {
        public static void WriteCSV(int index,string str)
        {
            string dirPath = "D:\\DataTest\\";

            if(!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            string filePath = dirPath + getTime() + "_" + index.ToString() + ".csv";
            
            if (!File.Exists(filePath))
            {
                FileStream fs = new FileStream(filePath,FileMode.Create);
                StreamWriter sw = new StreamWriter(fs,Encoding.UTF8);
                switch(index)
                {
                    case 0:
                        sw.WriteLine("时间,文件名称,X,Y");
                        break;
                    case 1:
                        sw.WriteLine("时间,文件名称,Circle_X,Circle_Y,Angle");
                        break;
                    case 2:
                        sw.WriteLine("时间,文件名称,Circle_X,Circle_Y,Angle");
                        break;
                }

                str = getTime() + "," + str;
                sw.WriteLine(str);
                sw.Close();
                fs.Close();
            }
            else
            {

                FileStream fs = new FileStream(filePath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs,Encoding.Default);
                str = getTime() + "," + str;
                sw.WriteLine(str);
                sw.Close();
                fs.Close();
            }
        }

        public static string getTime()
        {
            return DateTime.Now.Year.ToString().PadLeft(4,'0') + "_" +
                DateTime.Now.Month.ToString().PadLeft(2,'0') + "_" +
                DateTime.Now.Day.ToString().PadLeft(2,'0');
        }
    }
}
